﻿using FySystem.Extension;
using Microsoft.Extensions.Primitives;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FySystem.Net.HttpAuth
{
    public class HttpHeadManager
    {
        /// <summary>
        /// 从head中获取指定key的value
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public static string Get(KeyValuePair<string, StringValues>[] heads, string key)
        {
            string res = string.Empty;
            try
            {
                if (heads.IsNullOrEmpty())
                    return res;

                var head = heads.Where(x => x.Key == key).FirstOrDefault();
                res = head.Value;
            }
            catch (Exception)
            {
                throw;
            }
            return res;
        }

        public static TokenAndOpenID GetTokenAndOpenID(KeyValuePair<string, StringValues>[] heads)
        {
            TokenAndOpenID res = new TokenAndOpenID();
            try
            {
                if (heads.IsNullOrEmpty())
                    return res;

                foreach(var item in heads)
                {
                    if (item.Key == "token")
                        res.Token = item.Value;
                    else if (item.Key == "openid")
                        res.OpenID = item.Value;
                    else if (item.Key == "culture")
                        res.Culture = item.Value;
                }
            }
            catch (Exception)
            {
                throw;
            }
            return res;
        }

        public static Dictionary<string,string> GetAll(KeyValuePair<string, StringValues>[] heads)
        {
            Dictionary<string, string> res = new Dictionary<string, string>();
            try
            {
                if(heads.IsNullOrEmpty()==false)
                {
                    foreach(var item in heads)
                    {
                        if (res.ContainsKey(item.Key) == false)
                            res.Add(item.Key, item.Value);
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            return res;
        }
    }
}
